/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package main;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.xml.crypto.Data;
import main.Tree.ResultsTree;

/**
 *
 * @author Pacific
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException, Exception {
        String inPath = "in.txt" ;
        Main main = new Main() ;
        //main.generateMatrix(inPath, 5);
        List<String> lines = main.readLines(inPath) ;
        int[][] costMatrix = main.populateMatrix( lines ) ;
        System.out.println("Begin Matrix:\n"+Resulter.print(costMatrix)) ;
        
        Resulter resulter = new Resulter( costMatrix ) ;
        
        System.out.println(resulter);
        
        
        ResultsTree tree = new ResultsTree( resulter ) ;
        Resulter finishResult = tree.getFinishResult() ;
        
        System.out.println("\n\n********FINISH***********\n"+finishResult);
        
        int summ = 0 ;
        for( int i=0 ; i<costMatrix.length ; ++i ) {
            for( int j=i+1 ; j<costMatrix.length ; ++j ) {
                if( finishResult.getGoMatrix()[i][j] )
                    summ += costMatrix[i][j] ;
            }
        }
        System.out.println("Cost="+summ);
    }
    
    public List<String> readLines( String path ) throws FileNotFoundException, IOException {
        File inFile = new File( path ) ;
        
        FileReader fw =new FileReader( inFile ) ;
        BufferedReader br = new BufferedReader( fw ) ;
        
        ArrayList<String> lines = new ArrayList<>() ;
        String str ;
        while( null != (str=br.readLine()) ) {
            lines.add(str);
        }
        br.close() ;
        return lines ;
    }
    
    public int[][] populateMatrix( List<String> lines ) {
        int[][] costMatrix = null ;
        
        Pattern findDigits = Pattern.compile("\\d+");
        for( int lineNum=0 ; lineNum<lines.size() ; ++lineNum) {
            String str = lines.get( lineNum ) ;
            Matcher matcher = findDigits.matcher(str) ;
            
            ArrayList<Integer> digits = new ArrayList<>();
            while( matcher.find() ) {
                int value = Integer.valueOf( matcher.group() ) ;
                digits.add(value);
            }
            if( null == costMatrix ) {
                int size = digits.size() ;
                costMatrix = new int[size][size] ;
            }
            
            for( int columnNum=0 ; columnNum<digits.size() ; ++columnNum ) {
                costMatrix[lineNum][columnNum] = digits.get(columnNum) ;
            }
        }
        return costMatrix ;
    }
    
    
    public void generateMatrix( String path, int size) throws IOException {
        File file = new File( path ) ;
        FileWriter fw = new FileWriter( file ) ;
        Random random = new Random(new Date().getTime() ) ;
        
        int[][] matrix = new int[size][size] ;
        
        for( int i=0 ; i<size ; ++i ) {
            for( int j=0 ; j<=i ; ++j ) {
                int value = Math.abs( random.nextInt()%100 );
                matrix[i][j] = value ;
                matrix[j][i] = value ;
            }
        }
        
        for( int i=0 ; i<size ; ++i ) {
            for( int j=0 ; j<size ; ++j ) {
                fw.write(""+matrix[i][j]);
                if( j != size-1 )
                    fw.write( " " );
            }
            if( i != size-1 )
                fw.write( "\n" );
        }
        fw.close();
    }
}
